package com.i.template.handler;

import com.i.template.constant.ResponseResult;
import com.i.template.exception.BusinessException;
import jakarta.validation.ConstraintViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.net.BindException;

/**
 * @author lewis
 * @type class
 * @description
 * @since 2024/7/25
 */
@RestControllerAdvice
public class GlobalExceptionHandlerAdvice {
    /**
     * 自定义业务异常
     * @param exception
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseResult<Object> businessFailed(BusinessException exception){
        return new ResponseResult<>(exception.getCode(),exception.getMessage());
    }
    @ExceptionHandler(BindException.class)
    public ResponseResult<Object> bindError(BindException bindException) {
        return new ResponseResult<>(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase());
    }
    /**
     * 页面找不到
     * @param exception
     * @return
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ResponseResult<Object> notFound(NoHandlerFoundException exception){
        return new ResponseResult<>(HttpStatus.NOT_FOUND.value(),HttpStatus.NOT_FOUND.getReasonPhrase());
    }
    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseResult<Object>  constraintViolationExceptionHandler(ConstraintViolationException e) {
        return new ResponseResult<>(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase());
    }
    @ExceptionHandler(MissingPathVariableException.class)
    public ResponseResult<Object> handleMissingPathVariableException(MissingPathVariableException exception){
        return new ResponseResult<>(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase());
    }
    /**
     * 请求方式不受支持
     * @param exception
     * @return
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseResult<Object> methodNotAllowed(HttpRequestMethodNotSupportedException exception){
        return new ResponseResult<>(HttpStatus.METHOD_NOT_ALLOWED.value(),HttpStatus.METHOD_NOT_ALLOWED.getReasonPhrase());
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult<Object> notValid(MethodArgumentNotValidException exception) {
        return ResponseResult.fail();
    }
    /**
     * 全局拦截错误
     * @param exception
     * @return
     */
//    @ExceptionHandler(Exception.class)
//    public ResponseResult<?> error(Exception exception){
//        return ResponseResult.fail();
//    }
}
